package com.bhs.sansonglogistics.ui.orderDispose;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.AbnormalOrderBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.AbnormalOrderAdapter;
import com.bhs.sansonglogistics.ui.wallet.PayDeskActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;


/**
 * 异常订单列表
 */
public class AbnormalOrderListFragment extends BaseFragment implements NetCallBack {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private AbnormalOrderAdapter mAdapter;
    private int page = 1;
    private String type;

    public static AbnormalOrderListFragment newInstance(String type) {
        AbnormalOrderListFragment fragment = new AbnormalOrderListFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_abnormal_order_list;
    }

    @Override
    public void initView(View root) {
        type = getArguments().getString("type");
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvList = root.findViewById(R.id.rv_list);
        mAdapter = new AbnormalOrderAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        loadData();
    }

    @Override
    public void initData() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));


//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                int id = mAdapter.getData().get(position).getId();
//                startActivity(new Intent(getContext(), AbnormalOrderDetailActivity.class)
//                        .putExtra("id", id));
//            }
//        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AbnormalOrderBean.DataDTO.ListDTO data = mAdapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.btn_dispose:
                        startActivity(new Intent(getContext(), AbnormalOrderDisposeActivity.class)
                                .putExtra("id", data.getId()));
                        break;
                    case R.id.btn_payment:
                        startActivity(new Intent(getContext(), PayDeskActivity.class)
                                .putExtra("money", data.getHandle_money())
                                .putExtra("type", 4)
                                .putExtra("pay_sn", String.valueOf(data.getId())));
                        break;
                    case R.id.btn_check:
                        startActivity(new Intent(getContext(), AbnormalOrderDetailActivity.class)
                                .putExtra("id", data.getId()));
                        break;
                    case R.id.tv_phone:
                    case R.id.iv_phone:
                        MyUtils.callPhone(getContext(),data.getCargo_info().getMobile());
                        break;
                }
            }
        });
    }

    Map<String, String> map = new ArrayMap<>();

    private void loadData() {
        map.put("page", String.valueOf(page));
        map.put("limit", "10");
        map.put("statuss", type);
        networkRequest(netApi.get_abnormal_list(map), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        AbnormalOrderBean bean = new Gson().fromJson(json, AbnormalOrderBean.class);
        mRefreshLayout.setRefreshing(false);
        if (bean.isStatus()) {
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}